<?php

namespace App\Http\Controllers\Payment;

use App\Http\Controllers\Controller;
use App\Models\SellerWithdrawRequest;
use App\Models\Shop;
use App\Models\Wallet;
use Illuminate\Http\Request;

class FastPayController extends Controller
{


    public function notify(Request $request)
    {
        $input = $request->input();
        if (isset($input['status']) && $input['status'] == 'success'){
            $wallet = Wallet::findOrFail($input['order_no']);
            $wallet->approval = 1;
            if ($wallet->type == 2) {
                #print_r( $wallet->user->shop->toArray());
                #$wallet->user->shop->bzj_money+
                $shopid = $wallet->user->shop->id;
                $shop = Shop::findOrFail($shopid);
                $shop->bzj_money = $wallet->user->shop->bzj_money + $wallet->amount;
                $shop->save();

            } else {
                $user = $wallet->user;
                $user->balance = $user->balance + $wallet->amount;
                $user->save();
                $wallet->operator_id = 0;
            }
            if ( $wallet->save() )
            {
                return 'ok';
            }
        }
        return 'error';
    }


    public function fund_notify(Request $request)
    {
        $input = $request->input();
        if (isset($input['status'])){
            if ($input['status'] == 'success'){
                $seller_withdraw_request = SellerWithdrawRequest::findOrFail($input['order_no']);
                if ($seller_withdraw_request){
                    $seller_withdraw_request->status = '1';
                    $seller_withdraw_request->viewed = '1';
                    $seller_withdraw_request->save();
                }


                return 'ok';
            }

        }
        return 'error';
    }

}
